package com.act.moudles.user.controller;

import com.act.moudles.user.entity.SecretKey;
import com.act.moudles.user.service.SecretKeyService;
import com.act.utils.DataResult;
import com.act.utils.PageSearch;
import com.act.utils.validations.AddGroup;
import com.act.utils.validations.ValidResult;
import com.act.utils.validations.ValidationUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Api(tags = "密钥")
@RequestMapping("user/secretkey")
public class SecretkeyController {

    @Resource
    private SecretKeyService secretKeyService;



    @PostMapping("pageKey")
    @ApiOperation("分页查询")
    public DataResult pageKey(@RequestBody PageSearch<SecretKey> pageSearch){
        Page<SecretKey> page = Page.of(pageSearch.getPageNum(),pageSearch.getPageSize());
        return DataResult.success("查询成功",secretKeyService.pageKey(page,pageSearch.getDomain()));
    }


    @GetMapping("genSecretKey")
    @ApiOperation("生成当前人员的密钥")
    public DataResult genSecretKey(){
        return DataResult.success("生产成功！",secretKeyService.genSecretValue());
    }

    @PostMapping("addSecret")
    @ApiOperation("新增密钥")
    public DataResult  addSecret(@RequestBody SecretKey secretKey){
        // 校验接口参数
        ValidResult validResult = ValidationUtils.validateGroup(secretKey, AddGroup.class);
        if (validResult.isHasErrors()) {
            return DataResult.fail(validResult.getErrorMsg());
        }
        secretKeyService.addKey(secretKey);
        return DataResult.success("新增成功！ ");
    }


    @GetMapping("removeKey")
    public DataResult removeKey(Long id){
        secretKeyService.removeKey(id);
        return DataResult.success("删除成功！");
    }



}
